package com.block.datapush.service.apipush;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.model.param.PushApiKeyParam;
import com.block.common.service.IMemberCommonService;
import com.block.common.service.LogFactory;
import com.block.common.util.LocalDateUtil;
import com.block.datapush.enums.PushStatusEnum;
import com.block.datapush.model.PushQueryParam;
import com.block.datapush.model.dto.NoticeParamDTO;
import com.block.datapush.model.result.ProtocolParamResult;
import com.block.datapush.model.result.PushApiResult;
import com.block.datapush.service.IPushApiService;
import com.block.datapush.service.apipush.utils.QiHuiUtil;
import com.block.datapush.service.apipush.utils.SuiBianJieUtil;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import com.block.db.entity.MerchantDataPushInfo;
import com.block.db.mapper.MerchantDataPushInfoMapper;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.HashMap;
import java.util.Map;

/**
 *  随便借
 */
@Service
public class SuiBianJieApiService implements IPushApiService {

    private static final Logger log = LogFactory.getPushLog();

    @Autowired
    IMemberCommonService memberCommonService;


    @Override
    public PushApiResult checkUser(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {

        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);


        String queryUrl = pushApiKeyParam.getCheckUrl();


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", memberSourceInfo.getMobileMd5());

        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Authorization",  pushApiKeyParam.getApiSecret());
        headerMap.put("Content-Type", "application/json");



        log.info("【随便借】撞库开始【queryUrl】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createGet(queryUrl)
                .headerMap(headerMap,true)
                .form(paramMap)
                .execute().body();
        log.info("【随便借】撞库结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        log.info("撞库结果resultObject : {}",resultObject);
        Integer code = resultObject.getInteger("code");
        String message = resultObject.getString("message");

        pushApiResult.setCode(code + "|" );
        pushApiResult.setMessage(message);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(code), "0")) {
            pushApiResult.setPushSuccess(true);
        }
        return pushApiResult;
    }

    @Override
    public PushApiResult queryApprove(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {
        return null;
    }

    @Override
    public PushApiResult registerUser(String orderNo, String outOrderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        pushApiResult.setOrderNo(orderNo);

        String requestUrl = pushApiKeyParam.getRegisterUrl();
        long time = System.currentTimeMillis();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("phone", memberSourceInfo.getMobile());
        paramMap.put("name", memberSourceInfo.getRealname());
        paramMap.put("idnum", memberSourceInfo.getIdcard());
        paramMap.put("age", memberSourceInfo.getAge());
        paramMap.put("gender", memberSourceInfo.getSex()==1?'男':'女');
        paramMap.put("workingCity", memberSourceInfo.getCity());
        paramMap.put("gjj", SuiBianJieUtil.getGjj(memberSourceInfo.getGjj()));
        paramMap.put("shebao",SuiBianJieUtil.getshebao(memberSourceInfo.getShebao()));
        paramMap.put("credit", "无");
        paramMap.put("insurance", SuiBianJieUtil.getbaodan(memberSourceInfo.getBaoxian()));
        paramMap.put("house", SuiBianJieUtil.getHouse(memberSourceInfo.getHouse()));
        paramMap.put("car",SuiBianJieUtil.getCar(memberSourceInfo.getCar()));
        paramMap.put("huabei", SuiBianJieUtil.getHuabei(memberSourceInfo.getHuabei()));
        paramMap.put("baitiao", SuiBianJieUtil.getBaitiao(memberSourceInfo.getBaitiao()));
        paramMap.put("license", '无');
        paramMap.put("zhima", SuiBianJieUtil.getZhimaScore(memberSourceInfo.getZhima()));
        paramMap.put("creditRecord", SuiBianJieUtil.getyuqi(memberSourceInfo.getYuqi()));
        paramMap.put("time", time);
        paramMap.put("loanAmount",SuiBianJieUtil.getloan(memberSourceInfo.getLoanTime()));
        paramMap.put("source","淘金分期");

        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Authorization",  pushApiKeyParam.getApiSecret());
        headerMap.put("Content-Type", "application/x-www-form-urlencoded");



        log.info("【随便借】导单开始【queryUrl】{}【paramMap】{}", requestUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(requestUrl)
                .headerMap(headerMap,true)
                .form(paramMap)
                .execute().body();
        log.info("【随便借】导单结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        Integer code = resultObject.getInteger("code");
        String message = resultObject.getString("message");

        pushApiResult.setCode(code + "|" );
        pushApiResult.setMessage(message);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(code), "1")) {
            pushApiResult.setPushSuccess(true);
            pushApiResult.setSettleFlag(true);
            pushApiResult.setStatus(PushStatusEnum.SUCCESS.getValue());
            return pushApiResult;
        }
        pushApiResult.setPushSuccess(false);
        pushApiResult.setSettleFlag(false);
        pushApiResult.setStatus(PushStatusEnum.FAILED.getValue());
        return pushApiResult;
    }

    @Override
    public PushApiResult queryResult(PushQueryParam pushQueryParam, PushApiKeyParam pushApiKeyParam, String extJson) {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        return pushApiResult;
    }



    @Override
    public NoticeParamDTO getNoticeResultParam(String infoId, Map<String, String[]> parameterMap, String paramContent) {
        NoticeParamDTO noticeParamDTO = new NoticeParamDTO();
        return noticeParamDTO;
    }

    @Override
    public JSONObject getNoticeResultReturn(Map<String, String[]> parameterMap, String paramContent, JSONObject extObject) {
        JSONObject jsonObject = new JSONObject();
        return jsonObject;
    }

    @Override
    public ProtocolParamResult getProtocolResult(MerchantBaseInfo merchantBaseInfo, MemberSourceInfo memberSourceInfo) {
        ProtocolParamResult paramResult = new ProtocolParamResult();
        return paramResult;
    }
}
